package com.njtoyo.taxi.admin.service.business.operation_rule.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.cache.PrimaryCachePutter;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverRegionServiceArrangement;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverRegionServiceArrangementCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverRegionServiceArrangementMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.DriverServiceArrangementPresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.DesignatedChargeWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.DriverServiceArrangementAddUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.DriverServiceArrangementWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.DriverServiceArrangementService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DriverServiceArrangementServiceImpl implements DriverServiceArrangementService {

    @Autowired
    private DriverRegionServiceArrangementMapper driverRegionServiceArrangementMapper;

    @Autowired
    private PrimaryCachePutter primaryCachePutter;

    @Override
    public RestResult<IPage<DriverServiceArrangementPresenter>> page(AdminUser adminUser, DriverServiceArrangementWrapper wrapper) {
        wrapper.setServiceCategory(DriverRegionServiceArrangementCategory.fee_rate);
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case canton:
            case company:
                wrapper.setDriverRegionIds(identity.getDriverRegionIds());
                break;
        }

        IPage<DriverServiceArrangementPresenter> res = driverRegionServiceArrangementMapper.selectPage(wrapper);
        return RestResult.success(res);
    }

    @Override
    public RestResult<DriverServiceArrangementPresenter> detail(AdminUser adminUser, Long id) {
        DriverServiceArrangementPresenter res = driverRegionServiceArrangementMapper.selectDetailById(id);

        return RestResult.success(res);
    }

    @Override
    public RestResult deleteCache() {
        primaryCachePutter.deleteDriverServiceArrangement();
        return RestResult.success();
    }

    @Override
    public RestResult getPageByCategory(AdminUser adminUser, DriverServiceArrangementWrapper wrapper) {
        IPage<DriverServiceArrangementPresenter> pages = driverRegionServiceArrangementMapper.selectPage(wrapper);
        return RestResult.success(pages);
    }

    @Override
    public RestResult add(DriverServiceArrangementAddUpdateWrapper wrapper) {
        DriverRegionServiceArrangement entity = driverRegionServiceArrangementMapper.selectOne(Wrappers.<DriverRegionServiceArrangement>lambdaQuery()
                .eq(DriverRegionServiceArrangement::getServiceCategory, wrapper.getServiceCategory())
                .eq(DriverRegionServiceArrangement::getDriverRegionId, wrapper.getRegionId()));
        if (ObjectUtil.isNotNull(entity)) {
            return RestResult.failed("添加失败,运营区已存在");
        }
        driverRegionServiceArrangementMapper.insert(new DriverRegionServiceArrangement()
                .setDriverRegionId(wrapper.getRegionId())
                .setIsOpen(Boolean.TRUE)
                .setServiceCategory(wrapper.getServiceCategory())
                .setDetailSpecification(wrapper.getRuleStr()));
        primaryCachePutter.deleteDriverServiceArrangement();
        return RestResult.success();
    }

    @Override
    public RestResult updateDesigantedRuleIsOpen(DesignatedChargeWrapper wrapper) {
        driverRegionServiceArrangementMapper.updateById(new DriverRegionServiceArrangement()
                .setId(wrapper.getId())
                .setIsOpen(wrapper.getIsOpen()));
        return RestResult.success();
    }

    @Override
    public RestResult deleteById(Long id) {
        driverRegionServiceArrangementMapper.deleteById(id);
        primaryCachePutter.deleteDriverServiceArrangement();
        return RestResult.success();
    }

    @Override
    public RestResult update(DriverServiceArrangementAddUpdateWrapper wrapper) {
        DriverRegionServiceArrangement entity = driverRegionServiceArrangementMapper.selectOne(Wrappers.<DriverRegionServiceArrangement>lambdaQuery()
                .eq(DriverRegionServiceArrangement::getServiceCategory, wrapper.getServiceCategory())
                .eq(DriverRegionServiceArrangement::getDriverRegionId, wrapper.getRegionId())
                .not(i -> i.eq(DriverRegionServiceArrangement::getId, wrapper.getId())));
        if(ObjectUtil.isNotNull(entity)){
            return RestResult.failed("修改失败,运营区已存在");
        }
        driverRegionServiceArrangementMapper.updateById(new DriverRegionServiceArrangement()
                .setId(wrapper.getId())
                .setDriverRegionId(wrapper.getRegionId())
                .setDetailSpecification(wrapper.getRuleStr()));
        primaryCachePutter.deleteDriverServiceArrangement();
        return RestResult.success();
    }
}
